package com.jekunauto.retrofittest;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.jekunauto.retrofittest.实体.Repo;
import com.jekunauto.retrofittest.实体.UserInfo;
import com.jekunauto.retrofittest.实体.UserParam;
import com.jekunauto.retrofittest.实体.WeatherJson;
import com.jekunauto.retrofittest.请求接口.ApiStores;
import com.jekunauto.retrofittest.请求接口.ApiStoresThree;
import com.jekunauto.retrofittest.请求接口.ApiStoresTwo;
import com.jekunauto.retrofittest.请求接口.GitHubService;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class MainActivity extends AppCompatActivity {
    private TextView tv_content;
    private TextView tv_content_two;
    private TextView tv_content_three;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_content = (TextView) findViewById(R.id.tv_content);
        tv_content_two = (TextView) findViewById(R.id.tv_content_two);
        tv_content_three = (TextView) findViewById(R.id.tv_content_three);



        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://api.jekunauto.com/v1/order/")
                .addConverterFactory(GsonConverterFactory.create())
                .addConverterFactory(ScalarsConverterFactory.create())
                .build();
        GitHubService service = retrofit.create(GitHubService.class);
        Call<Repo> repos = service.getSearchBooks();
        //异步调用
        repos.enqueue(new Callback<Repo>() {
            @Override
            public void onResponse(Call<Repo> call, Response<Repo> response) {
                //主线程
                //注意这里用第一个Response参数的
//                String jsonString = String.valueOf(response.body());
                Log.e("onResponse-->", "ok");

            }
            @Override
            public void onFailure(Call<Repo> call, Throwable t) {
                Log.e("onFailure-->", t.getMessage());
            }
        });
    }

    /**
     * 获取天气信息
     * @param view
     */
    public void tq(View view) {
        Retrofit retrofit = AppClient.retrofit();
        ApiStores apiStores = retrofit.create(ApiStores.class);
        Call<WeatherJson> weather = apiStores.getWeather("101010100");
        weather.enqueue(new Callback<WeatherJson>() {
            @Override
            public void onResponse(Call<WeatherJson> call, Response<WeatherJson> response) {
                Log.i("wxl", "getWeatherinfo=" + response.body().getWeatherinfo().city);
                tv_content.setText("天气城市为=>> " + response.body().getWeatherinfo().city);
            }

            @Override
            public void onFailure(Call<WeatherJson> call, Throwable t) {
                Log.i("wxl", "Throwable=" + t.toString());
            }
        });
    }

    /**
     * 获取天气信息
     * @param view
     */
    public void tqq(View view) {
        Retrofit retrofit = AppClientThree.retrofit();
        ApiStoresThree apiStores = retrofit.create(ApiStoresThree.class);
        Observable<WeatherJson> observable = apiStores.getWeather("101010100");
        observable.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<WeatherJson>() {
                    @Override
                    public void onError(Throwable e) {
                        Log.i("wxl", "e=" + e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        Log.i("wxl", "onCompleted");
                    }

                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(WeatherJson weatherJson) {
                        tv_content_three.setText("天气城市为(结合Rxjava)=>> " + weatherJson.getWeatherinfo().toString());
                    }
                });
    }

    /**
     * 加载多个用户信息
     * @param view
     */
    public void yhsj(View view) {
        Retrofit retrofit = AppClientTwo.retrofit();
        ApiStoresTwo apiStoresTwo = retrofit.create(ApiStoresTwo.class);
        UserParam userparam = new UserParam();
        userparam.setHash("");
        userparam.setLongitude("113.434435");
        userparam.setLatitude("23.166524");
        userparam.setPage_size("20");
        userparam.setStart("0");

        Call<UserInfo> userInfo = apiStoresTwo.getUserInfo(userparam);

        userInfo.enqueue(new Callback<UserInfo>() {
            @Override
            public void onResponse(Call<UserInfo> call, Response<UserInfo> response) {
                tv_content_two.setText("请求到的信息为==>" + response.body().getList().get(0).toString());
            }

            @Override
            public void onFailure(Call<UserInfo> call, Throwable t) {
                Log.i("wxl", "Throwable=" + t.toString());
            }
        });
    }
}
